﻿using System;
using System.Data.SQLite;
using CompactMapper.Orm.Contracts;
using CompactMapper.Orm.UnitOfWork.Commands.Interfaces;
using CompactMapper.Orm.UnitOfWork.Mappings;
using System.Data;

namespace CompactMapper.Orm.UnitOfWork.Commands.Implementations
{
    internal sealed class GetByKeyCommand : Command, IGetByKeyCommand
    {
        private readonly String _cmdText;

        public GetByKeyCommand(Table table)
            : base(table)
        {
            _cmdText = new SqlBuilder().Query(table, new[] { "Key = @Key" }).ToString();
        }

        public SQLiteDataReader Run(SQLiteConnection connection, IKey key)
        {
            var cmd = new SQLiteCommand(connection);
            cmd.CommandText = _cmdText;
            cmd.Parameters.Add(new SQLiteParameter
                                   {
                                       ParameterName = "@Key",
                                       Value = key.Value
                                   });
            return cmd.ExecuteReader();
        }
    }
}